/**
 * @author:  maybe
 * @date: 2024/1/10 
 * @des:  微信支付
 */
const WxPay = require('wechatpay-node-v3'); // 支持使用require
const fs = require('fs');
const path = require("path");
const { wx} = require('./../../utils/config')

const apiclient_cert = path.resolve(__dirname, 'apiclient_cert.pem');
const apiclient_key = path.resolve(__dirname, 'apiclient_key.pem');
const pay = new WxPay({
    appid: wx.appid,//小程序appid
    mchid: wx.mch_id,//商户号
    publicKey: fs.readFileSync(apiclient_cert), // 公钥
    privateKey: fs.readFileSync(apiclient_key), // 秘钥
});

const wxPay  = async (row) =>{
    const params = {
        appid: wx.appid,
        mchid:  wx.mch_id,
        description: '订单支付',
        out_trade_no: row.orderNo,
        notify_url: 'https://api.yirishengxian.asia/api/web/self/notifyOrder',
        amount: {
            total: Math.ceil(Number(row.money)*100),//向上取整解决科学计数法问题
            currency: "CNY"
        },
        payer: {
            openid: row.userId
        }
    };
    const result = await pay.transactions_jsapi(params);
    return result
}

module.exports = {
    wxPay,
    pay
}